package graph;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import mapreduce.DynamicNumberFormat;
import mapreduce.NullFormat;
import mapreduce.Record;
import mapreduce.RecordFeeder;
import mapreduce.StringFormat;

public class PairRelationFeeder implements RecordFeeder {

	private BufferedReader m_reader;
	private Record m_record;

	public PairRelationFeeder(File dataFile){
		try {
			m_reader = new BufferedReader(new FileReader(dataFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public Record getRecord() {
		return m_record;
	}

	@Override
	public int next() {
		try {
			String line = m_reader.readLine();
			if(line == null){
				m_record = null;
				return -1;
			}
			String[] tokens = line.split(":");
			Record.RecordData key = new Record.RecordData(tokens, new DynamicNumberFormat(new StringFormat()));
			Record.RecordData value = new Record.RecordData(null, new NullFormat());
			m_record = Record.getRecord(key, value);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			m_record = null;
			return -1;
		}
		return 0;
		
	}

}
